import os
from os.path import *
import SCons.Errors

def PathListParse(path_list_string):
    if path_list_string == 'None':
	lst = []
    else:
	lst = path_list_string.split(os.pathsep)
    return lst

def PathListValidate(key, value, env):
    for dir in PathListParse(value):
	if not isdir(dir):
	    raise SCons.Errors.UserError('Not a valid directory: %s' % dir)

def PathListOption(key, help, default = None):
    return (key, help, default, PathListValidate, None)

def ParsePrefixes(env,env_var):
    if not env.has_key(env_var):
	return
    path_list = env[env_var]
    for dir in PathListParse(path_list):
        env.Append(CPPPATH=os.path.join(abspath(dir),'include'),
		   LIBPATH=os.path.join(abspath(dir),'lib'))
	# ParseConfig() breaks scons policy on user environment invariance
	# (scons 0.96)
	if os.environ.has_key('PKG_CONFIG_PATH'):
	    os.environ['PKG_CONFIG_PATH'] += os.pathsep
	else:
	    os.environ['PKG_CONFIG_PATH'] = ''
	    
	os.environ['PKG_CONFIG_PATH'] += os.path.join(abspath(dir),'lib','pkgconfig')


